package zooi;

import pinserver.*;
import java.awt.Dimension;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Main {

  public Main() {
    MyLogger logger = new MyLogger("pinserver.Main");
    logger.log(MyLogger.info, "Main (constructor)", "Bankserver wordt gestart !");
    new BankServer();
  }
  public static JTextArea txtAr;

  public static java.awt.Dimension getSchermresolutie() {
    return java.awt.Toolkit.getDefaultToolkit().getScreenSize();
  /* Voorbeeld code:
   * Dimension a = OokHandig.getSchermresolutie();
   * int breed = a.width;
   * int hoog = a.height;
   */
  }

  public static void drukaf(String str) {
    txtAr.setText(txtAr.getText() + "\n" + str);
    System.out.println(str);
  }

  public static void main(String[] args) {
    //// dingFrame
    JFrame dingFrame = new JFrame();
    txtAr = new JTextArea();
    txtAr.setText("Foo");
    txtAr.setEditable(false);
    txtAr.setFont(new Font("Monospaced", Font.PLAIN, 8));
    JScrollPane sp = new JScrollPane(txtAr);
    dingFrame.add(sp);
    dingFrame.setSize(250, 300);
    //Slash de regel hieronder als je m niet wilt zien
    dingFrame.setVisible(true);
    Dimension d = getSchermresolutie();
    dingFrame.setLocation(d.width - 250, 0);
    //// einde dingFrame
    //De Server Starten
    new Main();
  }
}

class BankServer {

  final int POORT = zooi.Settings.myPoort;
  ServerSocket server;
  Socket client;
  MyLogger logger;
  ArrayList PinClientDingenLijst = new ArrayList();

  public BankServer() {
    //De Server Starten :P
    logger = new MyLogger("pinserver.BankServer");
    //Poort openzetten
    try {
      server = new ServerSocket(POORT);
      logger.log(MyLogger.info, "BankServer (constructor)", "Server Wordt Gestart");
    } catch (Exception e) {
      logger.log(MyLogger.fatal, "BankServer (constructor)", "Poort niet beschikbaar, server geannuleerd");
      System.exit(0);
    }
    //Clients binnenlaten
    while (true) {
      try {
        client = server.accept();
        String adres1 = client.getInetAddress().toString();
        String adres2 = client.getRemoteSocketAddress().toString();
        logger.log(MyLogger.info, "BankServer (constructor)", "Nieuwe client " + adres1 + " " + adres2 + " geaccepteerd");
        PinClientDinges pcd = new PinClientDinges(client);
        PinClientDingenLijst.add(pcd);
        new Thread(pcd).start();
      } catch (Exception e) {
        logger.log(MyLogger.fatal, "BankServer (constructor)", "FOUT:\n" + e);
        System.exit(0);
      }
    }
  }

  /*
  public static void main(String[] args) {
  //   Wordt gestart via pinserver.Main.main(String[] args)
  new BankServer();
  }
   */
  public class PinClientDinges implements Runnable {

    BufferedReader lees;
    String tekst, ontvangen;
    PrintWriter schrijf;
    Socket client;

    public PinClientDinges(Socket s) {
      client = s;
    }

    public void run() {
      maakvariabelen();
      communiceerclient();
    }

    void maakvariabelen() {
      try {
        schrijf = new PrintWriter(client.getOutputStream(), true);
        lees = new BufferedReader(new InputStreamReader(client.getInputStream()));
      } catch (Exception e) {
        logger.log(MyLogger.error, "maakvariablen", "FOUT:\n" + e);
      }
    }

    void communiceerclient() {
      //zend("0x01");
      //ontvangen = ontvang();
      logger.log(MyLogger.info, "communiceerclient", "Start communicatie");
      while (!client.isClosed()) {
        ontvangen = ontvang();
        inputVerwerken(ontvangen);
      //broadcast("\"" + ontvangen + "\" reageer >: ");
      }
    }

    void inputVerwerken(String input) {
      //
      String[] dingen = input.split(" ");

    }

    void arrayPrinter(String[] a) {
    }

    void zend(String tekst) {
      schrijf.println(tekst);
      logger.log(MyLogger.info, "zend", "Verzenden = " + tekst);
    }

    public String ontvang() {
      String deTekst = "";
      try {
        deTekst = lees.readLine();
        if (deTekst.equals("0x02")) {
          logger.log(MyLogger.info, "ontvang", "Client gestopt (kreeg net 0x02 binnen)");
          client.close();
        } else {
          logger.log(MyLogger.info, "ontvang", "Ontvangen = " + deTekst + " [" + deTekst.length() + "]");
        }
      } catch (Exception e) {
        logger.log(MyLogger.error, "ontvang", "FOUT:\n" + e);
      }
      return deTekst;
    }

    void broadcast(String mes) {
      //Zend een bericht naar ALLE clients
      for (int i = 0; i < PinClientDingenLijst.size(); i++) {
        PinClientDinges b = (PinClientDinges) PinClientDingenLijst.get(i);
        b.zend(mes);
      }
    }
  }
}